<?php

class Task extends Controller{

	function task()
	{
		parent::Controller();
		$this->load->helper('url');
    $this->load->library('session');
		$this->load->model('task_model');
	}
	
	//to list states names 
  function list_task()
  {
    $user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
        $this->load->model('task_model');
        $this->load->helper('flexigrid');
                
          $displayform ='';
          $displayform .='<div id=dashboard ><span>Manage Task</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
          $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href='.base_url().'task/task_add >[Add New task]</a>&nbsp;<a href="'.base_url().'idea_home/logout">Logout</a></div>';
          $displayform .= '<br/>';
               
          $displayform .= '<table id="flex1" style="display:none" border=0 align=left ></table>';
          $displayform .= '<br/>';
        
        $colModel['task_id'] = array('Sl No',80,TRUE,'center',0);
        $colModel['task_desc'] = array('Task Description',200,TRUE,'left',2);
        
        $gridParams = array(
          'width' => 'auto',
          'height' => 400,
          'rp' => 15,
          'rpOptions' => '[10,20,30,40]',
          'pagestat' => 'Displaying:{from} to {to} of {total} items.',
          'blockOpacity' => 0.5,
          'title' => 'List of Tasks',
          'showTableToggleBtn' => true
        );
        
        $grid_js = build_grid_js('flex1',site_url("/ajax/task_list"),$colModel,'task_id','asc',$gridParams);
        
        $data['js_grid'] = $grid_js;
          
        $data = array('formdisplay'=>$displayform,'js_grid'=>$grid_js );  
        $this->template->add_css('css/flexigrid/flexigrid.css');
        $this->template->add_js('js/flexigride/jquery.pack.js');
        $this->template->add_js('js/flexigride/flexigrid.pack.js');
        
        //ends here....
        $this->template->write_view('header','header', $data, True);
        $this->template->write_view('content', 'general_flexigrid');
        $this->template->render();
     }
  else
  {
    echo "Forbidden to access!!";
  }      
    
  }
	
	function task_add()
	{
	  $user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
      
      		$task_name = array('name'=>'task_name','id'=>'task_name',
      				'type'=>'text','class'=>'required');
      		$this->load->helper('form');
      		$this->load->library('form_validation');
      		$displayform ='';
      		$displayform .='<div id=dashboard ><span>Manage Task</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
          $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
          $displayform .= '<br/>';
      		$displayform .='<fieldset style="width:50%;"><legend>New Task</legend>';
      		$attr = array('id'=>'new_task');
      		$displayform .= form_open('task/task_add',$attr);
      		$displayform .= '<table >';
      		$displayform .= '<tr><td align=left> Task Description:</td>';
      		$displayform .= '<td>'.form_input($task_name).'</td>';
      		$displayform .= '</tr>';
      		$displayform .= '<tr><td align=right colspan=2>'.form_submit('submit','Create New Task').' '.form_reset('cancel','Cancel').'</td>';
      		$displayform .= '</tr>';
      		$displayform .= '</table></fieldset>';
      		$displayform .=form_close();
      		$this->form_validation->set_rules('task_name', 'Task Name', 'trim|required|xss_clean');
      	
      		$data = array('formdisplay'=>$displayform );
      		//$this->template->add_js('js/jquery-1.3.2.js');
      		
      		if ($this->form_validation->run() == FALSE)
      		{		
      			$this->template->add_js('js/jquery-latest.js');	
      			$this->template->add_js('js/jquery.validate.js');
      			
      			$this->template->add_js('$(document).ready(function(){
      	 		   $("#new_task").validate();
      	  			});
      		','embed');
      			$this->template->write_view('header','header', $data, True);
      			$this->template->write_view('content', 'general');
      			$this->template->render();
      		}
      		else
      		{
      			
      			$this->task_model->insert_task();
      			
      			redirect('task/list_task/0');
      			
      		}
    		}
      else
      {
        echo "Forbidden to access";
      }

	}
	
	function duplicate_task()
	{
	  $user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
    		$displayform ='';
    		$displayform .='<div id=dashboard ><span>Manage Task</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
    		$displayform .= "This place already exist,please go back and try for another name!!";
    		
    		$data = array('formdisplay'=>$displayform );
    		$this->template->write_view('header','header', $data, True);
    			$this->template->write_view('content', 'other_general');
    			$this->template->render();
    }
    else
    {
      echo "Forbidden to access";
    }
	}
	
	function task_edit($id=0)
	{
	  $user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
    		$this->load->helper('form');
    		$task_name = $this->task_model->task_edit($id);
    		
    		$task = $task_name->result_array();
    		$task_name = array('name'=>'task_name','value'=>$task[0]['task_desc'],'type'=>'text','class'=>'required');
    		$displayform ='';
    		$displayform .='<div id=dashboard ><span>Manage Task</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
        $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
        $displayform .= '<br/>';
    		$displayform .='<fieldset style="width:50%;"><legend>Edit Task</legend>';
    		$attr = array('id'=>'edit_state');
    		$displayform .= form_open('task/save_edit',$attr);
    		$displayform .= '<input type=hidden name=id value='.$id.' />';
    		$displayform .= '<table class="tablestyle">';
    		$displayform .= '<tr><td align=left> Task Name:</td>';
    		$displayform .= '<td>'.form_input($task_name).'</td>';
    		$displayform .= '</tr>';
    		
    		$displayform .= '<tr><td align=center colspan=2>'.form_submit('submit','Update Task').'</td>';
    		$displayform .= '</tr>';
    		$displayform .= '</table></fieldset>';
    		$displayform .=form_close();
    			
    		$data = array('formdisplay'=>$displayform );
    		$this->template->write_view('header','header', $data, True);
    			$this->template->write_view('content', 'other_general');
    			$this->template->render();
		}
  else
  {
    echo "Forbidden to access!!";
  }

	}
	
	function save_edit()
	{
		$user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
    		$this->task_model->edit_save();
    		redirect('task/list_task/0');
    }
    else
    {
      echo "Forbidden to access!!";
    }
	}
  
}
